package com.alan.wifishare.presenter;

import android.text.TextUtils;

import com.alan.libapi.business.dao.CallbackListener;
import com.alan.libapi.model.business.UserSession;
import com.alan.library.allog.KLog;
import com.alan.wifishare.app.GlobalField;
import com.alan.wifishare.utils.BusinessUtil;
import com.alan.wifishare.utils.Md5Util;
import com.alan.wifishare.view.iview.IVUserRegister;

/**
 * @className: UserRegisterPS
 * @describtion: UserRegisterPS
 * @author: AlanWang
 * @email: 850267876@qq.com
 * @time: 2016/4/20 17:11
 */
public class UserRegisterPS {

    private IVUserRegister vRegister;

    public UserRegisterPS(IVUserRegister vRegister) {
        this.vRegister = vRegister;
    }

    /**
     * 提交手机号请求服务器发送验证码
     */
    public void sendVerifyCode() {
        String mobilePhone = vRegister.getUserPhone();
        if(!BusinessUtil.isMobileNum(mobilePhone)){
            vRegister.toastMsg("手机号输入错误");
            return;
        }
        // TODO: 对手机号加密

        GlobalField.businessHttp.postPhoneToRegisterVerify(mobilePhone, new CallbackListener<Void>() {
            @Override
            public void onSuccess(Void result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        vRegister.sendVerifyCodeSuccess();
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        vRegister.toastMsg(msg);
                    }
                });
            }
        });
    }

    public void submitRegisterInfo() {
        String userNicName = vRegister.getUserNicName();
        String userPhone = vRegister.getUserPhone();
        String verifyCode = vRegister.getVerifyCode();
        String userPwd = vRegister.getUserPwd();
        KLog.e("userNicName = " + userNicName);
        KLog.e("userPhone = " + userPhone);
        KLog.e("verifyCode = " + verifyCode);
        KLog.e("userPwd = " + userPwd);
        //验证各参数有效性
        if(TextUtils.isEmpty(userNicName)){
            vRegister.toastMsg("昵称不能为空");
            return;
        }
        if(!BusinessUtil.isMobileNum(userPhone)){
            vRegister.toastMsg("手机号输入错误");
            return;
        }
        if(TextUtils.isEmpty(verifyCode)){
            vRegister.toastMsg("验证码不能为空");
            return;
        }
        if(TextUtils.isEmpty(userPwd)){
            vRegister.toastMsg("密码不能为空");
            return;
        }

        // TODO: 对相关数据加密
        userPwd = Md5Util.stringToMd5(userPwd);
        KLog.e("userPwd = " + userPwd);
        vRegister.showProgress();
        GlobalField.businessHttp.postUserRegister(userNicName, userPwd, userPhone, verifyCode, new CallbackListener<UserSession>() {
            @Override
            public void onSuccess(final UserSession result) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        vRegister.hideProgress();
                        vRegister.registerSuccess(result);
                    }
                });
            }

            @Override
            public void onFailure(int errorCode, final String msg) {
                GlobalField.handler.post(new Runnable() {
                    @Override
                    public void run() {
                        vRegister.hideProgress();
                        vRegister.toastMsg(msg);
                    }
                });
            }
        });
    }


}
